console.log('\x1b[33m Welcome to the app! \x1b[0m');

/**
 * Bar object
 * @class
 */
class Bar {
  constructor(msg, time) {
    this.arrBar = new Array(10).fill('.');
    this.msg = msg;
    this.time = time;
    this.currentTime = 0;
  }

  /**
   * @method
   */
  update(percentageParam = null) {
    const percentage = percentageParam || (this.currentTime / this.time) * 100;
    if (percentage % 10 === 0) {
      for (let i = 0; i < percentage / 10; i += 1) {
        this.arrBar[i] = '=';
      }
    }
    this.drawingBar = '[' + this.arrBar.join('') + ']';
    return `${this.msg} ${Math.round(percentage)}% ${this.drawingBar}`;
  }

  /**
   * @method
   */
  start() {
    this.idTimeout = setInterval(() => {
      if (this.currentTime < this.time) {
        process.stdout.clearLine();
        process.stdout.cursorTo(0);
        process.stdout.write((`\x1b[36m ${this.update()} \x1b[0m`));
        this.currentTime += 1;
      }
    }, 1000);
  }

  /**
   * @method
   */
  async stop() {
    process.stdout.clearLine();
    process.stdout.cursorTo(0);
    process.stdout.write((`${this.update(100)}\n`));
    clearInterval(this.idTimeout);
  }
}

var bar = new Bar("222",1000);
bar.start();